<template>
  <div class="viewcontainer" v-if="dataset">
    <el-dialog
      :title="detailTitle"
      :visible.sync="detailFormVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <hi-form
        :dataSource="dataset"
        ref="detailForm"
        v-if="detailFormVisible"
        size="mini"
        :model="editData"
        v-bind="form"
      ></hi-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
    <el-row class="view-header">
      <el-col :span="22">
        <hi-lquery
          :columnsTotal="2"
          v-bind="queryPanel"
          :target="dataset"
        ></hi-lquery>
      </el-col>
      <el-col :span="2" style="text-align: right">
        <el-button type="primary" @click="showAdd">添加</el-button>
      </el-col>
    </el-row>
    <el-row class="view-main">
      <el-table
        ref="dataTable"
        :data="dataset.data"
        height="100%"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column type="index" width="50" />
        <el-table-column prop="FDESC" label="账户名称" width="180" />
        <el-table-column prop="FBANKDESC" label="银行名称" width="180" />
        <el-table-column prop="FBANKID" label="银行帐号" width="180" />
        <el-table-column prop="FACCOUNTNAME" label="户主姓名" width="180" />
        <el-table-column prop="FBANKADDR" label="银行地址" />
        <el-table-column prop="FCREATEDATE" label="创建时间" width="160" />
        <el-table-column prop="FUSERDESC" label="创建人" width="120" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showEdit(scope)">
              <i class="el-icon-edit"></i>
            </el-button>
            <el-button type="text" size="small" @click="remove(scope)">
              <i class="el-icon-delete"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="view-footer pagebar">
      <hi-pagination
        background
        :total="dataset.total"
        :target="dataset"
      ></hi-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  HiPagination,
  HiLquery,
  HiVquery,
  DataSetContainer,
  HiForm,
} from "hi-ui";
import viewBase from "@/views/base";
import moneyAccountDatacfg from "./dataset";
export default {
  name: "moneyAccountlist",
  extends: viewBase,
  components: { HiPagination, HiLquery, HiVquery, HiForm },
  data() {
    let datasetCfg = Object.assign(moneyAccountDatacfg, {
      action: "autoAction",
    });
    return {
      //数据桥梁
      dataBridge: new Map(),
      detailTitle: null,
      loading: false,
      //编辑状态
      editstate: "add", //add edit
      editData: {},
      detailFormVisible: false,
      dataset: null,
      form: {
        columnsTotal: 1,
        items: [
          {
            name: "FDESC",
          },
          {
            name: "FBANKDESC",
          },
          {
            name: "FBANKID",
          },
          {
            name: "FACCOUNTNAME",
          },
          {
            name: "FBANKADDR",
          },
        ],
      },
      queryPanel: {
        fields: [
          {
            element: "el-input",
            label: "账户名称",
            name: "FDESC",
            required: true,
          },
        ],
      },
      datasetCfg: datasetCfg,
      total: 100,
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    showAdd() {
      this.detailFormVisible = true;
      this.editstate = "add";
      this.detailTitle = "添加帐户";
      this.editData = this.dataset.createEmptyData();
    },
    showEdit(data) {
      this.dataset.curRecord = data.row;
      this.editstate = "edit";
      this.detailTitle = "编辑帐户-" + this.dataset.curRecord.FDESC || "";
      this.detailFormVisible = true;
      this.editData = Object.assign({}, this.dataset.curRecord);
    },
    remove(data) {
      this.$confirm("确定要删除【" + data.row.FDESC + "】", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let saveHelper = this.createSaveHelper(this.dataset.modelFile);
          saveHelper.remove(data.row);
          saveHelper.save().then((res) => {
            this.dataset.reload();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
          /*  this.dataset.remove(data.row);

          this.dataset.save().then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }); */
        })
        .catch(() => {});
    },
    save() {
      let _result;
      this.$refs.detailForm.validate((result) => {
        _result = result;
      });
      if (!_result) return;
      if (this.editstate == "edit") {
        let curModel = this.dataset.getCurModel();
        curModel.set(this.editData);
      } else {
        this.dataset.add(this.editData);
      }

      if (this.dataset.getDirty()) {
        let promise = this.dataset.save();
        promise.then((res) => {
          this.detailFormVisible = false;
          this.$message({
            message: "保存成功",
            type: "success",
          });
        });
      } else this.detailFormVisible = false;
    },
    afterquery() {
      this.dataset.load();
    },
  },
  mounted() {
    this.dataset = this.$dsContainer.createDataSet(this.datasetCfg);
    this.dataset.on("beginload", () => {
      this.loading = true;
    });
    this.dataset.on("afterload", () => {
      this.loading = false;
    });

    this.dataBridge.set(this.dataset, ["dataTable", "detailForm"]);
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
</style>

